package net.sf.nlp.ctb;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.util.List;

import net.sf.nlp.ltag.data.LTAGNode;
import net.sf.nlp.ltag.util.LTAG;

import org.apache.log4j.Logger;

public class CTBSegmentTraining 
{
	protected static Logger logger =  Logger.getLogger(CTBSegmentTraining.class);
	
	private TrieTreeNode trieDict = new TrieTreeNode();
	
	public void trainingDict(String path, final String extension)
	{
		File filePath = new File(path);
		
		if (!filePath.exists())
			logger.warn("The " + path + " is not existed!");
		
		File[] files = filePath.listFiles(new FileFilter() 
		{
			public boolean accept(File file) {
				if (file.isDirectory())
					return false;

				if (file.getName().toLowerCase().endsWith(extension))
					return true;

				return false;
			}
		});

		trieDict.clear();
		
		for (File file : files)
			trainingDict(file);
		
		trieDict.save("c:/core.txt");
		
		logger.info("Finished");
	}
	
	public void trainingDict(File ctbFile)
	{
		logger.info("training " + ctbFile.getName());
		
		try
		{
			BufferedReader reader = new BufferedReader(new FileReader(ctbFile));
			
			StringBuffer bracketedTree = new StringBuffer();
			String line = null;
			
			while ((line = reader.readLine()) != null)
			{
				if (line.trim().length() == 0)
				{
					trainingDict(bracketedTree.toString());
					
					bracketedTree.delete(0, bracketedTree.length());
				}
				else
				{
					bracketedTree.append(line).append("\r\n");
				}
			}
			
			reader.close();
		}
		catch(Exception ex)
		{
			logger.error("training " + ctbFile.getName());
			logger.error(ex, ex);
		}
	}
	
	private void trainingDict(String bracketedTree) throws Exception
	{
		if (bracketedTree == null)
			return;
		
		bracketedTree = bracketedTree.trim();
		if (bracketedTree.length() == 0)
			return;
		
		try
		{
			LTAGNode root = LTAG.buildLTAGNodeFromText(bracketedTree, true);
			
			logger.info("training :" + bracketedTree.replaceAll("\r\n", ""));
			trainingLTAGNode(root);
		}
		catch(Exception ex)
		{
			logger.error(bracketedTree);
			throw ex;
		}
	}
	
	private void trainingLTAGNode(LTAGNode node)
	{
		List<LTAGNode> children = node.getChildren();
		
		if (children == null || children.size() == 0)
		{
			if (node.getWord() != null)
				trieDict.addChild(node.getWord().getLexical(), node.getNodeText(), 1, false);
		}
		else
		{
			for (LTAGNode child : children)
				trainingLTAGNode(child);
		}
	}
	
	public static void main(String[] args)
	{
		CTBSegmentTraining training = new CTBSegmentTraining();
		
		training.trainingDict("C:/NLP/语料/ctb8.0/data/ctb/", ".ctb");
		//training.trainingDict(new File("C:/NLP/语料/ctb8.0/data/ctb/chtb_4182.ctb"));
	}
}
